﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JMT.Controls.Elements;

namespace JMT.Controls.Relations
{
    /// <summary>
    /// JLinkAggregation.xaml 的交互逻辑
    /// </summary>
    public partial class JLinkAggregation : UserControl, JLink
    {
        private IJBox _fromPanel;
        private IJBox _toPanel;
        private LinkDirection _direction;

        public IJBox FromPanel
        {
            get { return _fromPanel; }
        }
        public IJBox ToPanel
        {
            get
            {
                return _toPanel;
            }
        }

        public JMT.Data.JRelation Relation { get; set; }
        private Relations.LinkNodes.JAggregationNode _beginNode = new LinkNodes.JAggregationNode();

        public JLinkAggregation()
        {
            InitializeComponent();
            this._ctlCanvas.Children.Add(_beginNode);
        }

        public void Attach(IJBox from, IJBox to)
        {
            _fromPanel = from;
            _toPanel = to;

            GetXXYY2();
            _fromPanel.UpdateLayout += new Action(_toPanel_UpdateLayout);
            _toPanel.UpdateLayout += new Action(_toPanel_UpdateLayout);
        }

        void _toPanel_UpdateLayout()
        {
            GetXXYY2();
        }
        private void GetXXYY2()
        {
            Point fromCenter = _fromPanel.GetCenterPos();
            Point toCenter = _toPanel.GetCenterPos();
            JMT.Controls.Relations.Maths.LinkPosUtil.CalcLinkPos(_fromPanel.GetRect(), _toPanel.GetRect(),
                out fromCenter,
                out toCenter);

            _beginNode.SetVertex(fromCenter);
            _beginNode.SetDirection(toCenter, fromCenter);

            _ctlLine.X1 = fromCenter.X;
            _ctlLine.Y1 = fromCenter.Y;
            _ctlLine.X2 = toCenter.X;
            _ctlLine.Y2 = toCenter.Y;
        }
    }
}
       